package org.example;

import java.util.HashMap;
import java.util.Map;

public class topic169 {
    public static int majorityElementHashMap(int[] nums) {
        Map<Integer, Integer> countMap = new HashMap<>();
        int n = nums.length;
        for (int num : nums) {
            countMap.put(num, countMap.getOrDefault(num, 0) + 1);
            if (countMap.get(num) > n / 2) {
                return num;
            }
        }
        return -1; // 根据题意，数组一定存在多数元素，因此不会到达这里
    }

    public static void main(String[] args) {
        int[] nums = {2, 2, 1, 1, 1, 2, 2};
        System.out.println(majorityElementHashMap(nums)); // 输出: 2
    }
}
